package com.neu.his.service.registFee02.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.neu.his.bo.fee.FeeRec;
import com.neu.his.bo.refund.Item;
import com.neu.his.bo.refund.RefundQSend;
import com.neu.his.bo.refund.RefundRec;
import com.neu.his.core.Result;
import com.neu.his.entity.DiagRecords;
import com.neu.his.entity.DrugDisturb;
import com.neu.his.entity.NonDrugDisturb;
import com.neu.his.entity.Patient;
import com.neu.his.mapper.*;
import com.neu.his.service.registFee02.RefundService;
import com.neu.his.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
public class RefundImpl implements RefundService {
    @Autowired
    private DiagRecordMapper diagRecordMapper;
    @Autowired
    private DrugDisturbMapper drugDisturbMapper;
    @Autowired
    private NonDrugDisturbMapper nonDrugDisturbMapper;
    @Autowired
    private PatientMapper patientMapper;
    @Autowired
    private DrugMapper drugMapper;
    @Autowired
    private NonDrugMapper nonDrugMapper;

    @Override
    public Result getRefund(FeeRec feeRec) {
        List<Item> itemList = new ArrayList<>();
        QueryWrapper<Patient> queryWrapperP = new QueryWrapper<>();
        queryWrapperP.lambda().eq(Patient::getIdNumber, feeRec.getPatientId());
        List<Patient> patients = patientMapper.selectList(queryWrapperP);
        if (patients.size() == 0) {
            return Result.fail("该患者不存在！");
        }
        QueryWrapper<DiagRecords> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(DiagRecords::getPatientId, patients.get(0).getId());
        List<DiagRecords> diagRecordsList = diagRecordMapper.selectList(queryWrapper);
        for (DiagRecords diagRecords : diagRecordsList) {

            //对于药品，若未发药，则允许退还real_count
            List<DrugDisturb> drugDisturbs = new ArrayList<>();
            QueryWrapper<DrugDisturb> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.lambda().eq(DrugDisturb::getDiagId, diagRecords.getId());
            drugDisturbs = drugDisturbMapper.selectList(queryWrapper1);
            for (DrugDisturb drugDisturb : drugDisturbs) {
                if (drugDisturb.getRealCount().intValue() != 0) {
                    Item item = new Item();
                    item.setItemId(drugDisturb.getId());
                    item.setItemType("药品");
                    item.setTotalCost(drugDisturb.getRealCount());
                    if (drugDisturb.isHaveDeliver()) {
                        item.setIsUsed("是");
                    } else {
                        item.setIsUsed("否");
                    }
                    //获取药品名称
                    item.setItemName(drugMapper.selectById(drugDisturb.getDrugId()).getDrugName());
                    itemList.add(item);
                }
            }
            //对于非药品，若未进行(have_deliver)。则允许退还total_count
            QueryWrapper<NonDrugDisturb> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.lambda().eq(NonDrugDisturb::getDiagId, diagRecords.getId());
            List<NonDrugDisturb> nonDrugDisturbs = nonDrugDisturbMapper.selectList(queryWrapper2);
            for (NonDrugDisturb nonDrugDisturb : nonDrugDisturbs) {
                if (nonDrugDisturb.isPaid() == true) {
                    Item item = new Item();
                    item.setItemId(nonDrugDisturb.getId());
                    item.setItemType("非药品");
                    item.setTotalCost(nonDrugDisturb.getTotalCount());
                    if (nonDrugDisturb.isHaveDeliver()) {
                        item.setIsUsed("是");
                    } else {
                        item.setIsUsed("否");
                    }
                    //item.setUsed(nonDrugDisturb.isHaveDeliver());
                    //获取药品名称
                    item.setItemName(nonDrugMapper.selectById(nonDrugDisturb.getNonDrugId()).getNonDrugName());
                    itemList.add(item);
                }
            }
        }
        RefundQSend refundQSend = new RefundQSend();
        PageList pageList = new PageList(itemList, feeRec.getCurPage(), feeRec.getPageSize());
        refundQSend.setData(pageList);
        refundQSend.setPatient(patients.get(0));
        return Result.success(refundQSend);
    }

    @Override
    public Result reFund(RefundRec refundRec) {
        for (Item item : refundRec.getData()) {
            BigDecimal cost = new BigDecimal(0);
            Long diagId = new Long(0);
            if (item.getItemType().equals("药品")) {
                UpdateWrapper<DrugDisturb> updateWrapper = new UpdateWrapper<>();
                updateWrapper.lambda().eq(DrugDisturb::getId, item.getItemId())
                        .set(DrugDisturb::getRealCount, cost);
                drugDisturbMapper.update(null, updateWrapper);
                diagId = drugDisturbMapper.selectById(item.getItemId()).getDiagId();
            } else {
                UpdateWrapper<NonDrugDisturb> updateWrapper = new UpdateWrapper<>();
                updateWrapper.lambda().eq(NonDrugDisturb::getId, item.getItemId());
                updateWrapper.set("is_paid", false);
                nonDrugDisturbMapper.update(null, updateWrapper);
                diagId = nonDrugDisturbMapper.selectById(item.getItemId()).getDiagId();
            }
            cost = diagRecordMapper.selectById(diagId).getRealCost();
            cost = cost.subtract(item.getTotalCost());
            UpdateWrapper<DiagRecords> updateWrapper = new UpdateWrapper<>();
            updateWrapper.lambda().eq(DiagRecords::getId, diagId)
                    .set(DiagRecords::getRealCost, cost);
            diagRecordMapper.update(null, updateWrapper);
        }
        return Result.success();
    }
}
